<?php

class configSwitches 
{
    public function __construct($dbConnection)
    {
        $this->Connection = $dbConnection;
    }
    
    //Properties
    public $Connection; 
    public $Vlans;
    
    public function GetAllVlans()
    {
      $vlans = array();
      $queries = new mysqlqueries();
      $resultVlan = $this->Connection->query($queries->GetTable($queries->Vlan, "vlanNumber"));
      while ($rowVlan = mysqli_fetch_array($resultVlan)) 
      {
          array_push($vlans, $rowVlan);        
      }
      $this->Vlans = $vlans;
      return $vlans;
    }
    
    public function GetAllSwitches()
    {
        $switches = array();
        $queries = new mysqlqueries();
        $resultSwitch = $this->Connection->query($queries->GetTable($queries->Switch, "name"));
        while ($rowSwitch = mysqli_fetch_array($resultSwitch)) 
        {
            array_push($switches, new ConfigSwitch($rowSwitch['ID'], $this->Connection));    
        }
        return $switches;    
    }
    public function AddVlan($vlanNumber, $vlanName)
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Add($queries->Vlan, array($vlanName, $vlanNumber)));
        return true;  
    }
    
    public function GetSwitchesWithVlan($vlanID)
    {
        $queries = new mysqlqueries();
        $switches = array();
        $resultSwitchHasVlan = $this->Connection->query($queries->GetIndex($queries->Switch_has_vlan, $vlanID, "vlan_ID", null));
        while ($rowSwitchHasVlan = mysqli_fetch_array($resultSwitchHasVlan)) 
        {
            array_push($switches, new ConfigSwitch($rowSwitchHasVlan['switch_ID'], $this->Connection));
        }
        return $switches;      
    }
    
    public function removeVlan($vlanID) 
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Remove($queries->Vlan, "ID", $vlanID));
        return true;
        
    }
    public function removeSwitch($switchID)
    {
        $queries = new mysqlqueries();
        $tempSwitch = $this->Connection->query($queries->GetIndex($queries->Patchpanel, $switchID, 'switchPort_switch_ID', null));
        $message = "";
        $enable = true; 
        if(mysqli_fetch_array($tempSwitch))
        {
            $message = "Wird noch im Patchpanel verwendet. \n\n";
            $enable = false;
        }
        
        $tempSwitchPort = $this->Connection->query($queries->GetIndex($queries->Switchport, $switchID, 'switch_ID', null));
 
        $message .= "Folgende Geräte sind noch an diesem Switch:";
        while($rowSwitchPort = mysqli_fetch_array($tempSwitchPort))
        {
            $tempMac = $this->Connection->query($queries->GetIndex($queries->Mac, $rowSwitchPort['ID'], 'switchPort_ID', null));
            if($rowMac = mysqli_fetch_array($tempMac))
            {
                $enable = false;
                $tempDevice = $this->Connection->query($queries->GetIndex($queries->Device, $rowMac['device_ID'], 'ID', null));
                if($rowDevice = mysqli_fetch_array($tempDevice))
                {
                    $message .= "\n-".$rowDevice['name'];
                }
            }
        }       
        if($enable)
        {
        $message = "true";
            $this->Connection->query($queries->Remove($queries->Switchport, "switch_ID", $switchID));
            $this->Connection->query($queries->Remove($queries->Switch_has_vlan, "switch_ID", $switchID));
            $this->Connection->query($queries->Remove($queries->Switch, "ID", $switchID));
        return 'true';
        }
        return $message;
        
    }
}
